local qiaoge = fk.CreateSkill{
  name = "plum__qiaoge",
}

Fk:loadTranslationTable{
  ["plum__qiaoge"] = "鞘歌",
  [":plum__qiaoge"] = "手牌数最多、体力值最大或装备区内牌数最多的角色成为【杀】的目标后，你可以交给一名其他角色一张伤害类牌或重铸一张非伤害类牌，"
  .."然后使用者可以令符合上述条件的至多两名角色成为额外目标，若如此做，该【杀】被目标角色抵消时，你摸一张牌。",

  ["#plum__qiaoge-invoke"] = "鞘歌：交给一名角色一张伤害类牌或重铸一张非伤害类牌，令 %dest 可以为此%arg增加至多两个目标",
  ["#plum__qiaoge-choose"] = "鞘歌：你可以为此%arg增加至多两个目标",
}

---@param room Room
---@param player ServerPlayer
---@return boolean
local qiaogeFilter = function(room, player)
  local funcs = {
    function (p) return player:getHandcardNum() >= p:getHandcardNum() end,
    function (p) return player.hp >= p.hp end,
    function (p) return #player:getCardIds("e") >= #p:getCardIds("e") end,
  }
  for _, f in ipairs(funcs) do
    if table.every(room:getAlivePlayers(false), f) then
      return true
    end
  end
  return false
end

qiaoge:addEffect(fk.TargetConfirmed, {
  mute = true,
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(qiaoge.name) and data.card.trueName == "slash" and not target.dead
    and not (data.extra_data and data.extra_data.plum__qiaoge and table.contains(data.extra_data.plum__qiaoge, player.id)) then  --防止触发自己
      return qiaogeFilter(player.room, target)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local _, ret = room:askToUseActiveSkill(player, {
      skill_name = "plum__qiaoge_active",
      cancelable = true,
      prompt = "#plum__qiaoge-invoke::"..data.from.id..":"..data.card:toLogString(),
    })
    if ret then
      event:setCostData(self, {tos = ret.targets, cards = ret.cards})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local function filter(p)
      return not table.contains(data.tos[1], p) and
        data.card.skill:modTargetFilter(data.from, p, {}, data.card, {bypass_distances = true})
    end
    local cost_data = event:getCostData(self)
    local room = player.room
    room:notifySkillInvoked(player, qiaoge.name)
    player:broadcastSkillInvoke(qiaoge.name, math.random(1,2))
    data.extra_data = data.extra_data or {}
    data.extra_data.plum__qiaoge = data.extra_data.plum__qiaoge or {}
    table.insert(data.extra_data.plum__qiaoge, player.id)

    if #cost_data.tos > 0 then
      room:moveCardTo(cost_data.cards, Card.PlayerHand, cost_data.tos[1], fk.ReasonGive, qiaoge.name, nil, true, player)
    else
      room:recastCard(cost_data.cards, player, qiaoge.name)
    end

    if data.from.dead then return end
    local targets = table.filter(table.filter(room.alive_players, filter), function (p)
      return qiaogeFilter(room, p)
    end)

    if #targets == 0 then return end
    local tos = room:askToChoosePlayers(data.from, {
      targets = targets,
      max_num = 2,
      min_num = 1,
      skill_name = qiaoge.name,
      cancelable = true,
      prompt = "#plum__qiaoge-choose::"..data.from.id..":"..data.card:toLogString()
    })

    if #tos > 0 then
      table.forEach(tos, function (p) data:addTarget(p) end)
    end
  end
})

qiaoge:addEffect(fk.CardEffectCancelledOut, {
  mute = true,
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(qiaoge.name)
    and data.extra_data and data.extra_data.plum__qiaoge
    and table.contains(data.extra_data.plum__qiaoge, player.id)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player.room:notifySkillInvoked(player, qiaoge.name)
    player:broadcastSkillInvoke(qiaoge.name, math.random(3,4))
    player:drawCards(1, qiaoge.name)
  end
})

return qiaoge